package com.liuqh.tag.controller;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.liuqh.tag.dto.UploadInfoReqDto;
import com.liuqh.tag.vo.ResultObject;

import lombok.extern.slf4j.Slf4j;

/**
 * @Description TODO(这里用一句话描述这个类的作用)
 *
 * @author liuqinghua
 * @Date 2023-2-16
 */
@Slf4j
@RestController
public class UploadTestContoller {

    @RequestMapping("upload")
    public ResultObject submitMetarial(@RequestParam String name, String desc, @RequestPart List<MultipartFile> file) {
        System.out.println("name:" + name);
        System.out.println("desc:" + desc);
        int index = 1;
        for (MultipartFile f : file) {
            System.out.println("file" + index + "-" + f.getOriginalFilename());
            File newFile = new File("D:/test", f.getOriginalFilename());
            try {
                f.transferTo(newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            index++;
        }
        return ResultObject.success("上传成功！");
    }

    @RequestMapping("upload2")
    public ResultObject submitMetarial2(@ModelAttribute UploadInfoReqDto info, @RequestPart List<MultipartFile> file) {
        System.out.println("name:" + info.getName());
        System.out.println("desc:" + info.getDesc());
        int index = 1;
        for (MultipartFile f : file) {
            System.out.println("file" + index + "-" + f.getOriginalFilename());
            File newFile = new File("D:/test", f.getOriginalFilename());
            try {
                f.transferTo(newFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
            index++;
        }
        return ResultObject.success("上传成功！");
    }
}
